import React from "react";
import { DataAction, IProTableRef } from "vendor/components/pro-table-native";
import { getUserPageList, createUser, updateUserInfo, deleteUserInfo, getUserInfo } from "../../api";

export const getDataAction = (proTableRef: React.MutableRefObject<IProTableRef>, deptId: number): DataAction => ({
    fetchList: (params) => {
        const { page, pageSize, filter } = params;
        return new Promise((resolve, reject) => getUserPageList(Object.assign({page,pageSize, deptId}, filter || {})).then((resp) => resolve(resp.data)).catch((e) => reject(e)))
    },
    getItem: (rowKey) => {
        const userId = rowKey as number;
        return new Promise((resolve, reject) => getUserInfo(userId).then((resp) => {
            resolve({
                userId: resp.data.userId,
                nickName: resp.data.nickName,
                userName: resp.data.userName,
                deptId: resp.data.deptId,
                phonenumber: resp.data.phonenumber,
                email: resp.data.email,
                sex: resp.data.sex,
                status: resp.data.status,
                postIds: resp.data.postIds,
                roleIds: resp.data.roleIds,
                remark: resp.data.remark
            })
        }).catch((e) => reject(e)));
    },
    createItem: (params) => {
        return new Promise((resolve, reject) => createUser(params).then((resp) => {
            proTableRef.current.doFetchList();
            resolve(resp.data)
        }).catch((e) => reject(e)))
    },
    updateItem: (rowKey, params) => {
        params.userId = rowKey;
        return new Promise((resolve, reject) => updateUserInfo(params).then((resp) => {
            proTableRef.current.doFetchList();
            resolve(resp.data)
        }).catch((e) => reject(e)))
    },
    deleteItems: (selectedRowKeys) => {
        return deleteUserInfo(selectedRowKeys.map((item) => parseInt(item)))
    },
})


